package study.java.jdk.jdk8._3_comparator.then_comparing;

import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import study.java.jdk.jdk8.User;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Slf4j
public class _3_thenComparing_Test {

    private List<User> userLt;

    @Before
    public void init() {
        User u1 = new User("1003").setAge(11);
        User u2 = new User("1002").setAge(22);
        User u3 = new User("1001").setAge(38);
        User u4 = new User("1001").setAge(35);
        userLt = Arrays.asList(u1, u2, u3, u4);
    }

    /**
     * @param other Comparator
     * @return Comparator<T>
     */
    @Test
    public void test() {
        Comparator<User> cmpByAge = Comparator.comparing(User::getAge).reversed();
        Comparator<User> comparator = Comparator.comparing(User::getCode)
                .thenComparing(cmpByAge);
        Collections.sort(userLt, comparator);
        userLt.forEach(d -> {
            log.info(">>>>>> {}", d);
        });
    }
}
